﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_ScoreSub : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;
        int seq = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        public void ListInqSubjectCombo()
        {
            InqStdScoreComboBLL obj = new InqStdScoreComboBLL();

            DataTable dt = new DataTable();
            dt = obj.ListInqStdScoreCombo();
            cbInqSubjectCombo.DataSource = dt;
            cbInqSubjectCombo.DataBind();
        }
        
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            if (DataItem != null)
            {
                ListInqSubjectCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    txtSubScore.Text = DataBinder.Eval(DataItem, "score_level_name").ToString();
                    txtSubScoreMin.Text = DataBinder.Eval(DataItem, "sco_min").ToString();
                    txtSubScoreMax.Text = DataBinder.Eval(DataItem, "sco_max").ToString();
                    txtPercentPromote.Text = DataBinder.Eval(DataItem, "percen_promote").ToString();
                    cbInqSubjectCombo.SelectedValue = DataBinder.Eval(DataItem, "std_dtls_seq").ToString();
                    SetMinMaxScore(Convert.ToInt32(DataBinder.Eval(DataItem, "std_dtls_seq").ToString()));
                    lbUpdateId.Text = DataBinder.Eval(DataItem, "sub_sco_level_seq").ToString();
                }            

            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void MessageAlert(Page p, string strMessage)
        {
            ScriptManager.RegisterClientScriptBlock(p, p.GetType(), "onclientclick"
                , "window.alert('" + strMessage + "');", true);
        }
       
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            BLL.ScoreSubBLL score = new ScoreSubBLL();
            try
            {
                if (String.IsNullOrEmpty(cbInqSubjectCombo.SelectedValue) ||
                    String.IsNullOrEmpty(txtSubScoreMin.Text.Trim()) ||
                    String.IsNullOrEmpty(txtSubScoreMax.Text.Trim()))
                {
                    //Utils.Web.MessageAlert(this.Page, "เป็นค่าว่างไม่ได้!!");
                    MessageAlert(this.Page, "เป็นค่าว่างไม่ได้!!");
                   return; 
                }
                if (score.ChkValidateScore(Convert.ToInt32(cbInqSubjectCombo.SelectedValue.ToString())
                                              , double.Parse(txtSubScoreMin.Text.Trim())
                                              , double.Parse(txtSubScoreMax.Text.Trim())))
                {
                    MessageAlert(this.Page, "กรอกช่วงคะแนนไม่ถูกต้อง!!");
                    return;
                }

            score.Insert(Int32.Parse(Session["PRMID"].ToString())
                                 , Int32.Parse(Session["JSID"].ToString())
                                 , Convert.ToInt32(cbInqSubjectCombo.SelectedValue.ToString())
                                 , txtSubScore.Text.Trim()
                                 , Convert.ToDecimal(txtSubScoreMin.Text.Trim())
                                 , Convert.ToDecimal(txtSubScoreMax.Text.Trim())
                                 , Convert.ToDecimal(txtPercentPromote.Text.Trim())
                                 , ssMng.EmpSeqI);
                }
            
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.ScoreSubBLL score = new ScoreSubBLL();
            try
            {
                if (score.ChkValidateScore(Convert.ToInt32(cbInqSubjectCombo.SelectedValue.ToString())
                    , double.Parse(txtSubScoreMin.Text.Trim())
                    , double.Parse(txtSubScoreMax.Text.Trim())))
                {
                    MessageAlert(this.Page, "กรอกช่วงคะแนนไม่ถูกต้อง!!");
                    return;
                }
                score.Update(Int32.Parse(Session["PRMID"].ToString())
                    , Int32.Parse(Session["JSID"].ToString())
                    , Convert.ToInt32(cbInqSubjectCombo.SelectedValue.ToString())
                    , txtSubScore.Text.Trim()
                    , Convert.ToDecimal(txtSubScoreMin.Text.Trim())
                    , Convert.ToDecimal(txtSubScoreMax.Text.Trim())
                    , Convert.ToDecimal(txtPercentPromote.Text.Trim())
                    , ssMng.EmpSeqI
                    , Int32.Parse(lbUpdateId.Text.Trim())
                    );
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
           
        }

        protected void cbInqSubjectCombo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (cbInqSubjectCombo.SelectedIndex > 0)
                SetMinMaxScore(Convert.ToInt32(cbInqSubjectCombo.SelectedValue.ToString()));
            else
                SetMinMaxScore();
        }

        private void SetMinMaxScore(int p)
        {
            try
            {
                InqStdScoreComboBLL obj = new InqStdScoreComboBLL();
                string[] score = obj.GetMaxMin(p);
                lblScoreMin.Text = score[0].ToString();
                lblScoreMax.Text = score[1].ToString();
            }
            catch (Exception ex)
            {
              
            }
        }

        protected void SetMinMaxScore()
        {
            lblScoreMin.Text = "";
            lblScoreMax.Text = "";
        }
    }
}